package servlet;

import mapper.Singuptext;
import model.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;

@WebServlet("/singup")
public class singupServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession httpSession = request.getSession();
        if (httpSession.getAttribute("Random") != null){
            Test test = new Test();
            test.setName(request.getParameter("name"));
            test.setText(request.getParameter("text"));
            Singuptext singuptext = new Singuptext();
            if (singuptext.Addtext(test)){
                response.getWriter().write("true");
            }else {
                response.getWriter().write("false");
            }
        }else {
            response.getWriter().write("false");
        }

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession httpSession = request.getSession();
        Random random = new Random();
        String num = String.valueOf(random.nextInt(1000));
        httpSession.setAttribute("Random",num);

        request.getRequestDispatcher("html/singup.html").forward(request,response);
    }
}
